<?php

namespace app\admin\controller\order;

use app\common\controller\Backend;
use app\admin\model\goods\Goods as GoodsModel;
use app\admin\model\hospital\Hospital as HospitalModel;

/**
 * 项目订单主单
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{

    /**
     * Order模型对象
     * @var \app\admin\model\order\Order
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\order\Order;
        $this->view->assign("orderTypeList", $this->model->getOrderTypeList());
        $this->view->assign("payTypeList", $this->model->getPayTypeList());
        $this->view->assign("payStatusList", $this->model->getPayStatusList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("reportStatusList", $this->model->getReportStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        $user = $this->auth->getUserInfo();
        $groupIds = [];
        foreach($this->auth->getGroupIds() as $key=>$value){
            $groupIds[] = $value;
        }
        $whereCustom = [];
        if(in_array(3,$groupIds,true) && count($groupIds) == 1){
            // 医院账号
            $hospital = \app\admin\model\hospital\Hospital::where('admin_id',$user['id'])->find();
            if($hospital){
                $whereCustom = [
                    'order.hospital_id' => $hospital['id'],
                ];
            }else{
                $whereCustom = [
                    'order.hospital_id' => -1,
                ];
            }
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->alias('order')
            ->field('order.*')
            ->where($where)
            ->where($whereCustom)
            ->where('pay_status',1)
            ->join('user','order.user_id=user.id')
            ->with(['ordergoods','hospital','user','sale','orderTube'])
            ->order($sort, $order)
            ->paginate($limit);

        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    public function getGoodsInfo(){
        $user_id = $this->request->post('user_id','');
        $ids = $this->request->post('ids','');
        $goods_package_id = $this->request->post('goods_package_id','');

        if(!empty($goods_package_id)){
            $goods_package = \app\admin\model\goods\GoodsPackage::where('id','in',$goods_package_id)->where('deletetime',null)->select();
            if (!$goods_package) {
                $this->error('套餐不存在');
            }
            $goods_ids = [];
            foreach ($goods_package as $key => $value) {
                $goods_ids[] = $value['goods_ids'];
            }
            $goodsList = GoodsModel::where('id', 'in', implode(',',$goods_ids))->select();
        }else{
            $goodsList = GoodsModel::where('id', 'in', $ids)->select();
        }

        $user = \app\admin\model\User::get($user_id);
        if (empty($user->hospital_id)) {
            $this->error('请先绑定医院');
        }
        $hospital = HospitalModel::get($user->hospital_id);
        $goodsIds = explode(',', $hospital->goods_ids);
        $totalPrice = 0;
        $price = 0;
        $rongqiList = [];
        $rongqiIds = [];
    }


}
